package com.portal.rabbitmq.demo.consumer.service;

import com.alibaba.fastjson.JSON;
import com.portal.rabbitmq.demo.constants.MessageConstants;
import com.portal.rabbitmq.demo.consumer.entity.OrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单服务
 *
 * @author Amadeus
 * @date 2021/05/18
 */
@Slf4j
@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(String userId, String productId) {
        //组装消息
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setName(userId);
        orderDTO.setOrderId(productId);
        log.info("接收的消息入参:{}", JSON.toJSONString(orderDTO));
        //TODO 消息校验

        //发送到队列
        rabbitTemplate.convertAndSend(MessageConstants.EXCHANGE_NAME,
                MessageConstants.ROUTING_KEY, JSON.toJSONString(orderDTO));
    }
}
